//
//  TeamController.m
//  dfdsf
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "TeamController.h"
#import "SignInController.h"
#import "ManageController.h"
#import <DKNightVersion.h>
#import "DataBaseHandle.h"
#import "TeamView.h"
#import "ModelForManage2.h"
#import "NetworkingHandlerOfBlock.h"
#import "MyTeamController.h"
#import "UIColor+Wonderful.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define WEIGHT [UIScreen mainScreen].bounds.size.width

@interface TeamController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *labelForName;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array2;

@end

@implementation TeamController

- (void)dealloc {

    [_seg release];
    [_array release];
    [_button release];
    [_labelForName release];
    [_tableView release];
    [_array2 release];
    [super dealloc];
    
}

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
     self.seg.hidden = NO;
    
    if ([[DataBaseHandle shareDataBaseHandle] select].count == 0){
        
        self.array = [NSMutableArray array];
        [self.view viewWithTag:1000].hidden = NO;
        self.tableView.hidden = YES;
        
    }
    else{
        
        self.array = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
        [self.view viewWithTag:1000].hidden = YES;
        self.tableView.hidden = NO;
    }
    
    [self.tableView reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self createTitle];
    [self createSubview];

   
    
}

/**创建头部标题部分*/
- (void)createTitle {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor deepSkyBlue], [UIColor lightGrayColor]);
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"我的球队", @"签到排行"]];
    self.seg.frame = CGRectMake(100 * WEIGHTT, 0, self.view.frame.size.width - 200 * WEIGHTT, 40 * HEIGHTT);
    [self.navigationController.navigationBar addSubview:self.seg];
    [_seg release];
    
    self.seg.selectedSegmentIndex = 0;
    
    
    self.seg.layer.cornerRadius = 0;
    self.seg.layer.masksToBounds = YES;
    
    //去掉颜色,现在整个segment都看不见
    self.seg.tintColor = [UIColor clearColor];
    self.seg.selectedSegmentIndex = 0;
    
    // 设置选中时候的 segment 的属性
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName: [UIColor redColor]};
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    // segment 没有被选中的时候的属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-anonymous-iconfont-2"] style:UIBarButtonItemStylePlain target:self action:@selector(addTeamAction)];
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
    [self.seg addTarget:self  action:@selector(itemClick:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)itemClick:(UISegmentedControl *) seg {
    
    switch (seg.selectedSegmentIndex) {
            
        case 0:
            
            [self.navigationController popToRootViewControllerAnimated:NO];
            
            break;
            
        default:
            
            
        {
            
            SignInController *signIn = [[SignInController alloc] init];
            
            
            [self.navigationController pushViewController:signIn animated:NO];
            [signIn release];
            
            
            break;
            
            
        }
            
    }
    
}


- (void)addTeamAction {


    ManageController *manage = [[ManageController alloc] init];
    self.seg.hidden = YES;
    [self.navigationController pushViewController:manage animated:YES];
    
    [manage release];
    
    
}



- (void)createSubview {

    
    // 还没添加球队的 button 和 label
    
    UIView * viewOfBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:viewOfBack];
    [viewOfBack release];
    viewOfBack.tag = 1000;
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    //self.button.backgroundColor = [UIColor yellowColor];
    self.button.frame = CGRectMake(0, 0, self.view.frame.size.width / 4.7, self.view.frame.size.width / 4.7);
    self.button.center = self.view.center;
    [viewOfBack addSubview:self.button];
    
    [self.button setImage:[UIImage imageNamed:@"iconfont-shoucang-2"] forState:UIControlStateNormal];
    
    [self.button addTarget:self action:@selector(addNewsTeam:) forControlEvents:UIControlEventTouchUpInside];
    
    self.labelForName = [[UILabel alloc] initWithFrame:CGRectMake(80 * WEIGHTT, self.view.center.y + self.button.frame.size.height / 2 + 10 * HEIGHTT, self.view.frame.size.width - 160 * WEIGHTT, self.view.frame.size.height / 8)];
    [viewOfBack addSubview:self.labelForName];
    [_labelForName release];
    
    
    self.labelForName.text = @"您还没有关注的球队\n点击添加关注的球队吧";
    self.labelForName.numberOfLines = 0;
    self.labelForName.textAlignment = NSTextAlignmentCenter;
    self.labelForName.font = [UIFont systemFontOfSize:20];
    self.labelForName.textColor = [UIColor grayColor];
    
    // 创建 tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WEIGHT,HEIGHT - 113) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[TeamView class] forCellReuseIdentifier:@"poolForTeamView"];
    
   

}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.array.count;

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TeamView *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForTeamView"];
    
   ModelForManage2 *model = [self.array objectAtIndex:indexPath.row];

    NSString *url = @"http://platform.sina.com.cn/sports_uc/singleHometeamRank?app_key=2586208540&_version__=3.2.1.01&__os__=android&teamid=";
    NSString *url3 = @"&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
    NSString *url2 = [url stringByAppendingFormat:@"%@%@", model.sl_id, url3];
    
   // NSLog(@"%@", url2);
    
    [NetworkingHandler networkingWithUrl:url2 completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       
        NSDictionary *dic = [result objectForKey:@"result"];
        
        NSDictionary *dict  = [dic objectForKey:@"data"];
        
         cell.number.text = [dict objectForKey:@"rank"];
        
        
    }];
    
    
    [cell passModel:model];
    
    return cell;

}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.frame.size.height / 3;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    MyTeamController *team = [[MyTeamController alloc] init];
    
    ModelForManage2 *model = [self.array objectAtIndex:indexPath.row];
        
    NSString *url = @"http://platform.sina.com.cn/sports_client/feed?app_key=2586208540&_version__=3.2.1.01&__os__=android&team_id=";
    NSString *url2 = model.sl_id;
    NSString *url3 = @"&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info&num=20&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
    
    team.url = [url stringByAppendingFormat:@"%@%@", url2, url3];
    
    [self.navigationController pushViewController:team animated:YES
     ];
    
    [team release];


}


-(void)addNewsTeam:(UIButton *) button {

    self.seg.hidden = YES;
    ManageController *manage = [[ManageController alloc] init];
    [self.navigationController pushViewController:manage animated:YES];
    [manage release];
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
